-- @owner: @zou_jialiang050
-- @date: 2022/02/24
-- @testpoint: 在删除src表元素之前触发删除des表中id1<100的元素 each row

--step1:建表;expect:成功
drop table if exists t_trigger_src cascade;
drop table if exists t_trigger_des cascade;
create table t_trigger_src(id1 int, id2 int, id3 int) with (storage_type=ustore);
create table t_trigger_des(id1 int, id2 int, id3 int) with (storage_type=ustore);

--step2:在删除src表元素之前触发删除des表中id1<100的元素;expect:成功
create or replace function tri_delete_func() returns trigger as
$$
declare
begin
delete from t_trigger_des where id1=old.id1;
return old;
end
$$ language plpgsql;
/
drop trigger if exists delete_trigger on t_trigger_src;
create trigger delete_trigger before delete on t_trigger_src
for each row
when(old.id1<100)
execute procedure tri_delete_func();
/
insert into t_trigger_src values(1,1,1),(2,2,2);
insert into t_trigger_des values(1,1,1),(2,2,2);
select * from t_trigger_des order by id1;
delete from t_trigger_src where id1=1;
select * from t_trigger_src order by id1;
select * from t_trigger_des order by id1;

--step3:清理环境;expect:成功
drop table if exists t_trigger_src cascade;
drop table if exists t_trigger_des cascade;